package com.lwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.dao.BookCommentMapper;
import com.lwy.dao.BookNoveMapper;
import com.lwy.entity.BookCategory;
import com.lwy.entity.BookComment;
import com.lwy.entity.BookUser;
import com.lwy.service.BookCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
//评论表service接口实现类
@Service
public class BookCommentServiceImpl extends ServiceImpl<BookCommentMapper, BookComment> implements BookCommentService {


    @Resource
    private BookUserServiceImpl bookUserService;

    public List<BookComment> getComment(Integer nove_id, Integer chapter_id) {
        //放入所有的评论
        List<BookComment> list = new ArrayList<>();
        //查询所有的评论
        List<BookComment> bookComments = baseMapper.selectList(new QueryWrapper<BookComment>().eq("nove_id", nove_id).eq("chapter_id", chapter_id).eq("user_id", 0).eq("status",1));
        if (bookComments.size() > 0) {
            for (BookComment bookComment : bookComments) {
                //如果为空那么该对象为评论
                BookUser bookUser = bookUserService.getById(bookComment.getUid());
                bookComment.setBookUser(bookUser);
                list.add(bookComment);
            }
            return list;
        } else {
            return null;
        }
    }

    public List<BookComment> findById(Integer id) {

        List<BookComment> list = new ArrayList<>();
        List<BookComment> bookComments = baseMapper.selectList(new QueryWrapper<BookComment>().eq("id", id).eq("user_id",0).eq("status",1));
        for (BookComment bookComment : bookComments) {
            BookUser bookUser = bookUserService.getById(bookComment.getUid());
            bookComment.setBookUser(bookUser);
            list.add(bookComment);
        }

        for (BookComment bookComment : list) {
            List<BookComment> bookComments2 = baseMapper.selectList(new QueryWrapper<BookComment>().eq("user_id", bookComment.getId()).eq("nove_id", bookComment.getNove_id()).eq("chapter_id", bookComment.getChapter_id()).eq("status",1));
            for (BookComment bookComment1 : bookComments2) {
                BookUser bookUser = bookUserService.getById(bookComment1.getUid());
                bookComment1.setBookUser(bookUser);
            }
            bookComment.setList(bookComments2);
        }

        return list;
    }
}
